package com.chat.websocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

/**
 * websocket的配置处理器
 * 
 * @author change
 *
 */
@Configuration
@EnableWebMvc
@EnableWebSocket
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
	
	@Autowired
	private SystemHandler handler;

	/**
	 * 配置ws访问路径
	 */
	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		registry.addHandler(handler, "/ws/{userName}/{token}").addInterceptors(new WebSocketHandshakeInterceptor());
	}

	/**
	 * 以注解方式自动创建ServletServerContainerFactoryBean，配置属性、运行时消息缓冲区的大小尺寸、闲置超时等
	 * 
	 * @return
	 */
	@Bean
	public ServletServerContainerFactoryBean createWebSocketContainer() {
		ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
		// 最大文本信息缓存
		container.setMaxTextMessageBufferSize(8192);
		// 最大二进制信息缓存
		container.setMaxBinaryMessageBufferSize(8192);
		return container;
	}

}
